﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OpenPhotoAlbum.Controls;

namespace OpenPhotoAlbum.My
{
	public partial class Settings : AlbumPage
	{
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			this.Title = Resources.Strings.Title_My_Settings;

			tabPanels.TabPanelItems.Add(new TabPanelItem()
			{
				Name = Resources.Strings.My_Settings_ChangePassword,
				PanelClientId = pnlChangePassword.ClientID,
				Visible = false
			});
			tabPanels.TabPanelItems.Add(new TabPanelItem()
			{
				Name = Resources.Strings.Settings_UpdateProfile,
				PanelClientId = pnlUpdateProfile.ClientID,
				Visible = false
			});
			tabPanels.TabPanelItems.Add(new TabPanelItem()
			{
				Name=Resources.Strings.Settings_MyIcon,
				PanelClientId = pnlIcon.ClientID,
				Visible = false
			});

			if (!IsPostBack) { tabPanels.SetVisibleIndex(UrlContext.VisibleIndex); }

			ltrOldPassword.Text = StringUtility.AddColon(Resources.Strings.Settings_OldPassword);
			ltrNewPassword1.Text = StringUtility.AddColon(Resources.Strings.Settings_NewPassword);
			ltrNewPassword2.Text = StringUtility.AddColon(Resources.Strings.Settings_ConfirmNewPassword);
			btnSaveNewPassword.Text = Resources.Strings.Settings_SaveNewPassword;
			ltrEmail.Text = StringUtility.AddColon(Resources.Strings.Settings_EmailAddress);
			btnSaveProfile.Text = Resources.Strings.Settings_SaveProfile;

			btnSaveNewPassword.Click += new EventHandler(btnSaveNewPassword_Click);
			btnSaveProfile.Click += new EventHandler(btnSaveProfile_Click);

			message.Text = string.Empty;
			messageIcon.Text = string.Empty;
			

			btnSaveIcon.Text = Resources.Strings.Settings_SaveNewIcon;

			btnSaveIcon.Click += new EventHandler(btnSaveIcon_Click);
		}

		protected void btnSaveIcon_Click(object sender, EventArgs e)
		{
			if (fileUpload.FileBytes == null) { return; }

			byte[] icon = BusinessLogics.PhotoManager.Instance.ProcessUserIcon(fileUpload.FileBytes);
			BusinessLogics.UserManager.Instance.UpdateUserIcon(this.UserId, icon);

			tabPanels.SetVisibleIndex(2);
		}

		protected void btnSaveNewPassword_Click(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(tbOldPassword.Text)
				|| string.IsNullOrEmpty(tbNewPassword1.Text)
				|| string.IsNullOrEmpty(tbNewPassword2.Text))
			{
				message.Text = Resources.Strings.Settings_IncompleteInput;
				message.MessageType = MessageTypes.FailMessage;
			}
			else if (!tbNewPassword1.Text.Equals(tbNewPassword2.Text, StringComparison.CurrentCulture))
			{
				message.Text = Resources.Strings.Settings_InconsistentNewPasswords;
				message.MessageType = MessageTypes.FailMessage;
			}
			else if (!BusinessLogics.UserManager.Instance.ValidateUser(User.Identity.Name, tbOldPassword.Text))
			{
				message.Text = Resources.Strings.Settings_WrongOldPassword;
				message.MessageType = MessageTypes.FailMessage;
			}
			else
			{
				BusinessLogics.UserManager.Instance.ChangePassword(UserId, tbNewPassword1.Text);
				message.Text = Resources.Strings.Settings_SaveNewPasswordSuccess;
				message.MessageType = MessageTypes.SuccessMessage;
			}
			tabPanels.SetVisibleIndex(0);
		}

		protected void btnSaveProfile_Click(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(tbEmail.Text))
			{
				messageProfile.Text = Resources.Strings.Settings_IncompleteInput;
				messageProfile.MessageType = MessageTypes.FailMessage;
			}
			else
			{
				BusinessLogics.UserManager.Instance.ChangeEmail(UserId, tbEmail.Text);
				messageProfile.Text = Resources.Strings.Settings_SaveProfileSuccess;
				messageProfile.MessageType = MessageTypes.SuccessMessage;
			}
			tabPanels.SetVisibleIndex(1);
		}

		protected override void AppendSiteMapNodes(AlbumSiteMapProvider provider)
		{
			base.AppendSiteMapNodes(provider);

			provider.AppendSiteMapNode(Resources.Strings.SC_Home, Links.Default);
			provider.AppendSiteMapNode(Resources.Strings.My_Home, UrlContext.BuildMyHomeUrl(0));
			provider.AppendSiteMapNode(Resources.Strings.My_Settings, RawUrl);
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			if (!IsPostBack)
			{
				DbAccess.User user = BusinessLogics.UserManager.Instance.GetUser(UserId);
				tbEmail.Text = user.UserEmail;
			}
		}

		public string CurrentIconUrl
		{
			get { return UrlContext.BuildPhotoPipeUserIconUrl(this.UserId); }
		}
	}
}
